****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************
	
clear all
set more off
set matsize 800

global lotcons "female black white hispanic lunch?? lunch_miss app2 app3 anyt109 anyt110 anyt111 anyt112 anyt1lunch?? esly??"  

global nccons "math_lag read_lag science_lag math_gr_lag read_gr_lag mardgr_int_lag science_gr_lag allgr_int_lag i.lowread_nonmag#i.year lep_Y_lag ma_aig_lag rd_aig_lag aig_int_lag"

** Kindergarten	
use all-lottery-movers-analysis.dta , clear 

keep if (year>2008 & ksample & stayer==1)

gen mathstand_diff = mathstand_as - mathstand_at
gen readstand_diff = readstand_as - readstand_at

gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_as - avgtestscores_at

gen notmove1 = (1 - move1)

gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1

gen move1_lost = (1 - wonfirst)*move1

gen notmove1_lost = notmove1*(1 - wonfirst)

xtset lottery

tempfile ksample 
	save `ksample' 
	
su avgtestscores_as if wonfirst==1
local wonmaas = r(mean)
local wonsemaas = r(sd)

su avgtestscores_as if wonfirst==0
local lostmaas = r(mean)
local lostsemaas = r(sd)

su avgtestscores_at if wonfirst==1
local wonmaat = r(mean)
local wonsemaat = r(sd)

su avgtestscores_at if wonfirst==0
local lostmaat = r(mean)
local lostsemaat = r(sd)

su avgscore_diff if wonfirst==1
local wonmadiff = r(mean)
local wonmadiffse = r(sd)

su avgscore_diff if wonfirst==0
local lostmadiff = r(mean)
local lostmadiffse = r(sd)

reg avgtestscores_as wonfirst , cluster(lottery) 
local unconasmadiff = _b[wonfirst]
local unconasmase = _se[wonfirst]

reg avgtestscores_at wonfirst , cluster(lottery) 
local unconatmadiff = _b[wonfirst]
local unconatmase = _se[wonfirst]

reg avgscore_diff wonfirst , cluster(lottery) 
local unconmadid = _b[wonfirst]
local unconmadidse = _se[wonfirst]

	
xtreg avgtestscores_as wonfirst $lotcons , fe cluster(lottery) 

estadd scalar N_ind = e(N)

estadd scalar won = `wonmaas'
estadd scalar wonse = `wonsemaas'

estadd scalar lost = `lostmaas'
estadd scalar lostse = `lostsemaas'

estadd scalar diff = `unconasmadiff' 
estadd scalar diffse = `unconasmase' 

est store G

xtreg avgtestscores_at wonfirst $lotcons , fe cluster(lottery)

estadd scalar N_ind = e(N)

estadd scalar won = `wonmaat'
estadd scalar wonse = `wonsemaat'

estadd scalar lost = `lostmaat'
estadd scalar lostse = `lostsemaat'

estadd scalar diff = `unconatmadiff' 
estadd scalar diffse = `unconatmase' 

est store H

xtreg avgscore_diff wonfirst $lotcons , fe cluster(lottery)

estadd scalar N_ind = e(N)

estadd scalar won = `wonmadiff'
estadd scalar wonse = `wonmadiffse'

estadd scalar lost = `lostmadiff'
estadd scalar lostse = `lostmadiffse'

estadd scalar diff = `unconmadid'
estadd scalar diffse = `unconmadidse'


est store I

  
  
** Sixth Grade
	
use all-lottery-movers-analysis.dta , clear 

keep if (year>2008 & sixsample & stayer==1)

gen mathstand_diff = mathstand_as - mathstand_at
gen readstand_diff = readstand_as - readstand_at

gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_as - avgtestscores_at

gen notmove1 = (1 - move1)

gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1

gen move1_lost = (1 - wonfirst)*move1

gen notmove1_lost = notmove1*(1 - wonfirst)

xtset lottery

tempfile sixsample 
	save `sixsample'
  
su avgtestscores_as if wonfirst==1
local wonmaas = r(mean)
local wonsemaas = r(sd)

su avgtestscores_as if wonfirst==0
local lostmaas = r(mean)
local lostsemaas = r(sd)

su avgtestscores_at if wonfirst==1
local wonmaat = r(mean)
local wonsemaat = r(sd)

su avgtestscores_at if wonfirst==0
local lostmaat = r(mean)
local lostsemaat = r(sd)

su avgscore_diff if wonfirst==1
local wonmadiff = r(mean)
local wonmadiffse = r(sd)

su avgscore_diff if wonfirst==0
local lostmadiff = r(mean)
local lostmadiffse = r(sd)

reg avgtestscores_as wonfirst , cluster(lottery) 
local unconasmadiff = _b[wonfirst]
local unconasmase = _se[wonfirst]

reg avgtestscores_at wonfirst , cluster(lottery) 
local unconatmadiff = _b[wonfirst]
local unconatmase = _se[wonfirst]

reg avgscore_diff wonfirst , cluster(lottery) 
local unconmadid = _b[wonfirst]
local unconmadidse = _se[wonfirst]
	
xtreg avgtestscores_as wonfirst $lotcons $nccons , fe cluster(lottery) 

estadd scalar N_ind = e(N)

estadd scalar won = `wonmaas'
estadd scalar wonse = `wonsemaas'

estadd scalar lost = `lostmaas'
estadd scalar lostse = `lostsemaas'

estadd scalar diff = `unconasmadiff' 
estadd scalar diffse = `unconasmase' 

est store J

xtreg avgtestscores_at wonfirst $lotcons $nccons , fe cluster(lottery)

estadd scalar N_ind = e(N)

estadd scalar won = `wonmaat'
estadd scalar wonse = `wonsemaat'

estadd scalar lost = `lostmaat'
estadd scalar lostse = `lostsemaat'

estadd scalar diff = `unconatmadiff' 
estadd scalar diffse = `unconatmase' 

est store K

xtreg avgscore_diff wonfirst $lotcons $nccons , fe cluster(lottery)

estadd scalar N_ind = e(N)

estadd scalar won = `wonmadiff'
estadd scalar wonse = `wonmadiffse'

estadd scalar lost = `lostmadiff'
estadd scalar lostse = `lostmadiffse'

estadd scalar diff = `unconmadid'
estadd scalar diffse = `unconmadidse'


est store L

  
 esttab G H I J K L using table3.tex , replace ///
mtitles("Assigned" "Attended" "Difference" "Assigned" "Attended" "Difference") ///
label booktabs nonum gaps noobs f collabels(none) cells(none) ///
stats(won wonse lost lostse diff diffse , fmt(%9.3f %9.3f %9.3f %9.3f %9.3f %9.3f) ///
layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{(@)}" \addlinespace "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{(@)}" \addlinespace "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{(@)}") ///
  labels(`"   Won"' `" "' `" "' `"   Lost"' `" "' `" "' `"   Unconditional Diff"' `" "')) 


esttab G H I J K L using table3.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain ///
star(* 0.10 ** 0.05 *** 0.01) keep(wonfirst) coef(wonfirst "   Conditional Difference") ///
cells("b(fmt(3)star)" "se(fmt(3)par)") /// 
  
